package com.apes.framework.query;

import com.apes.framework.jpa.Connector;
import com.apes.framework.jpa.generator.service.SequenceService;
import com.apes.framework.query.lib.PubToolUtil;
import com.apestech.framework.db.SqlSession;
import com.apestech.framework.dictionary.modul.Table;
import com.apestech.framework.json.JSONArray;
import com.apestech.framework.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 动态SQL执行类的方法合集，其它类可直接注入之后引用它的方法
 *
 * @author hup
 * @create 2018-03-16-11:37
 */
@Service("sqlService")
public class SqlService extends PubToolUtil {

    @Autowired
    Connector connector;
    @Autowired
    SequenceService sequenceService;

    private SqlSession session=null;

    private SqlSession getSession() {
        try {
            this.session= getSession(connector.getConnection());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return this.session;
    }

    public JSONArray queryId(String queryid, JSONObject param) {
        JSONArray list=null;
        try {
            list=this.getSession().getForJSON().queryId(queryid, param);
            return list;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public JSONArray query(String sql) {
        JSONArray list=null;
        try {
            list=this.getSession().getForJSON().query(sql);
            return list;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    //public JSONArray query(String sql, JSONObject param, String extString);

    public JSONArray query(String sql, JSONObject param){
        JSONArray list=null;
        try {
            list=this.getSession().getForJSON().query(sql, param);
            return list;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

   public int update(String sql, JSONObject param) {
        int i=0;
        try {
            i=this.getSession().getForUpdate().update(sql, param);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return i;
    }

    public int update(String sql){
        int i=0;
        try {
            i=this.getSession().getForUpdate().update(sql);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return i;
    }

    public int insertTable(String tabName, JSONObject param){
        int i=0;
        try {
            i=this.getSession().getForUpdate().insertTable(tabName, param);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return i;
    }

    public int insertTable(String tabName, JSONObject param, String filter, boolean in) {
        int i=0;
        try {
            i=this.getSession().getForUpdate().insertTable(tabName, param, filter, in);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return i;
    }

    public void createTable(String tableName, String tableComment, JSONArray colList) {
        try {
            //this.getSession().getForDDL().createTable(tableName, tableComment, colList);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public void createTable(String tableName, JSONArray colList) {
        createTable(tableName, tableName, colList);
    }

    public void dropTable(String tableName)  {
        try {
            this.getSession().getForDDL().dropTable(tableName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public boolean tableIsEmpty(String tableName) {
        try {
            return this.getSession().getForResultSet().tableIsEmpty(tableName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public boolean tableExists(String tableName) {
        try {
            return this.getSession().tableExists(tableName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public Table getDictTable(String tableName) {
        try {
            return this.getSession().getDictTable(tableName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public JSONObject setColMap(String id, String name, String type, boolean isKey, boolean notNull, String defaultValue){
        JSONObject map=new JSONObject();
        map.put("id",id);
        map.put("name",name);
        map.put("type",type);
        map.put("key",isKey);
        map.put("notnull", notNull);
        map.put("default", defaultValue);
        return map;
    }

}
